//
//  ViewController.swift
//  Control Fun
//
//  Created by MacPro on 15/11/4.
//  Copyright © 2015年 dev4shane. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var nameTextField: UITextField!
    
    
    @IBOutlet weak var numberTextField: UITextField!
    
    
    @IBOutlet weak var sliderLabel: UILabel!
    
    @IBOutlet weak var leftSwitch: UISwitch!
    
    @IBOutlet weak var rightSwitch: UISwitch!
    
    
    @IBOutlet weak var doSomethingButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        sliderLabel.text = "50"
        doSomethingButton.hidden = true
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func textFieldDoneEditing(sender:UITextField){
        sender.resignFirstResponder()
    }
    
    @IBAction func backgroundTap(sender:UIControl){
        nameTextField.resignFirstResponder()
        numberTextField.resignFirstResponder()
    }
    
    
    @IBAction func sliderChanged(sender: UISlider) {
        
        let progress = lrintf(sender.value)
        sliderLabel.text = "\(progress)"
    }
    
    
    
    @IBAction func switchChanged(sender: UISwitch) {
        
        let setting = sender.on
        leftSwitch.setOn(setting, animated:true)
        rightSwitch.setOn(setting, animated:true)
    }
    

    @IBAction func toggleControls(sender: UISegmentedControl) {
        
        if sender.selectedSegmentIndex == 0 {
            leftSwitch.hidden = false
            rightSwitch.hidden = false
            doSomethingButton.hidden = true
            
        }else {
            leftSwitch.hidden = true
            rightSwitch.hidden = true
            doSomethingButton.hidden = false
        }
    }
    
    @IBAction func buttonPressed(sender: UIButton){
        
        let controller = UIAlertController(title: "Are You Sure?", message:nil, preferredStyle: .ActionSheet)
        
       let yesAction = UIAlertAction(title: "Yes, I'm sure!", style: UIAlertActionStyle.Destructive) { action in
        let msg = ((self.nameTextField.text!) == "") ? "You can breathe easy, everything went OK." : "You can breathe easy, \(self.nameTextField.text!), " + "everything went OK."
        let controller2 = UIAlertController(title:"Something  Was Done", message:msg, preferredStyle:.Alert)
        let cancelAction = UIAlertAction(title:"Phew!", style:.Cancel, handler:nil)
        controller2.addAction(cancelAction)
        self.presentViewController(controller2, animated:true, completion:nil)
        
        }
        
        let noAction = UIAlertAction(title:"No way!", style:.Cancel, handler:nil)
        controller.addAction(yesAction)
        controller.addAction(noAction)
        
        
        //for ipad
        if let ppc = controller.popoverPresentationController {
            ppc.sourceView = sender
            ppc.sourceRect = sender.bounds
            ppc.permittedArrowDirections = .Down
        }
        presentViewController(controller, animated:true, completion:nil)
    }
  
}

